package mapper;

import org.apache.ibatis.annotations.*;
import pojo.User;

import java.util.List;

public interface UserMapper {
    /**
     * 基础CRUD
     */
    @Select("select * from user")
    public List<User> findAll();
    @Select("select * from user where id = #{id}")
    public User findById(int id);
    @Insert("insert into user(username,password,birthday) values (#{username},#{password},#{birthday})")
    public void add(User user);
    @Update("update user set username = #{username},password = #{password},birthday = #{birthday} where id = #{id}")
    public void update(User user);
    @Delete("delete from user where id = #{id}")
    public void delete(int id);

    /**
     * 一对多多表查询
     * 通过用户查询用户信息和该用户下的订单信息
     */
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(
                    column = "id",//根据哪个字段去查询orders表的数据
                    property = "orderList",//要封装的属性名称
                    javaType = List.class,//要封装的实体类型
                    many = @Many(select = "mapper.OrderMapper.findByUid")
            )
    })
    public List<User> findUserAndOrderAll();

    /**
     * 多对多多表查询
     * 一个用户可以有多个角色，一个角色可以有多个用户
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(
                    many = @Many(select = "mapper.RoleMapper.findByUserId"),
                    javaType = List.class,
                    property = "roleList",
                    column = "id"
            )
    })
    public List<User> findUserAndRoleAll();
}
